package cn.cdqf.controller;

import cn.cdqf.bean.User;
import cn.cdqf.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "获取用户列表", notes = "获取全部用户")
    @GetMapping("/users")
    public List<User> queryAllUser(){
        List<User> users = userService.queryAllUser();
        return users;
    }

    @ApiOperation(value = "获取用户", notes = "根据id获取用户")
    @GetMapping("/users/{id}")
    public User queryUserById(@PathVariable("id") int id){
        User user = userService.queryUserById(id);
        return user;
    }

    @ApiOperation(value = "更新用户", notes = "根据id更新用户")
    @PutMapping("/users/{id}")
    public String updateUser(User user,@PathVariable("id") int id){
        int i = userService.updateUser(user, id);
        if (i>0){
            return "更新成功";
        }else {
            return "更新失败";
        }
    }

    @ApiOperation(value = "删除用户", notes = "根据id删除用户")
    @DeleteMapping("/users/{id}")
    public String deleteUser(@PathVariable("id")int id){
        int i = userService.deleteUser(id);
        if (i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @ApiOperation(value = "添加用户", notes = "新增用户")
    @PostMapping("/users")
    public String addUser(User user){
        int i = userService.addUser(user);
        if (i>0){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }
}
